﻿using MediatR;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using SmartProduction.Domain;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands;
using SmartProduction.Write.API.Application.Commands.JobCommand_工作中心;

namespace SmartProduction.Write.API.Application.CommandHandlers.JobCommandHandler_工作中心
{
    public class JobEditStatusCommandHandler : IRequestHandler<JobEditStatusCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Job> jobRepository;

        public JobEditStatusCommandHandler(IBaseRepository<Job> jobRepository)
        {
            this.jobRepository = jobRepository;
        }
        public async Task<APIResult<object>> Handle(JobEditStatusCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            
            var entiyi=jobRepository.GetAll().Result.FirstOrDefault(x=>x.JId==request.JId);
            entiyi.Status=request.Status;
           var i= jobRepository.UpdateAsync(entiyi).Result;
            if (i > 0)
            {
                result.Code = APIEnums.修改成功;
                result.Msg = APIEnums.修改成功.ToString();
            }
            else
            {
                result.Code = APIEnums.修改失败;
                result.Msg = APIEnums.修改失败.ToString();
            }
            result.data= i;
            return await Task.FromResult(result);
        }
    }
}
